package com.cicc.beergamevirtualsimulationsystem.common.webSocket.handler.impl;

import com.cicc.beergamevirtualsimulationsystem.common.webSocket.config.WebSocketMessageSender;
import com.cicc.beergamevirtualsimulationsystem.common.webSocket.handler.JsonMessageHandler;
import com.cicc.beergamevirtualsimulationsystem.common.webSocket.message.Impl.GameRoomDismissJsonWebSocketMessage;
import com.cicc.beergamevirtualsimulationsystem.common.webSocket.message.WebSocketMessageTypeEnum;
import com.cicc.beergamevirtualsimulationsystem.entity.RoomInfoManager;
import com.cicc.beergamevirtualsimulationsystem.service.RoomService;
import jakarta.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.socket.WebSocketSession;

@Component
public class GameRoomDismissJsonMessageHandler implements JsonMessageHandler<GameRoomDismissJsonWebSocketMessage> {

   @Autowired
    private  RoomService roomService;
    @Override
    public void handle(WebSocketSession session, GameRoomDismissJsonWebSocketMessage message) {
        //通知当前房间所有让房间解散
        String roomId = session.getAttributes().get("roomId").toString();
        WebSocketMessageSender.sendMessageToRoom(roomId,new GameRoomDismissJsonWebSocketMessage("房间已解散"));
        RoomInfoManager.clearRoom(roomId,0);
        roomService.closeRoom(roomId);

    }

    @Override
    public String type() {
        return WebSocketMessageTypeEnum.GAME_ROOM_DISMISS.getValue();
    }

    @Override
    public Class<GameRoomDismissJsonWebSocketMessage> getMessageClass() {
        return GameRoomDismissJsonWebSocketMessage.class;
    }
}
